'use strict';

/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 初始化状态机
 * zengbinsi
 * 2020-08-24
 */


import path from 'path';
import pathUtil from '../libs/utils/path-utils';
import fileUtil from '../libs/utils/file-utils';
import { callSequence } from '../libs/utils/function-call-seq';
const srcPath = pathUtil.getPathInTemplateAssets(path.join('bundles', 'framework', 'other', 'state-machine'));
const dstPath = pathUtil.getPathInAssets(path.join('bundles', 'framework', 'other', 'state-machine'));





// 初始化目录
const initDir = (callback?: Function) => {
    pathUtil.mkdirsSyncInAssets(path.join('bundles', 'framework', 'other', 'state-machine'), callback);
};

const initSMStateMachineChangeStateParams = (callback?: Function) => {
    console.log(`[tool-box]import SMStateMachineChangeStateParams.ts...`);
    fileUtil.copy(path.join(srcPath, 'SMStateMachineChangeStateParams.tsz'), path.join(dstPath, 'SMStateMachineChangeStateParams.ts'));
    console.log(`[tool-box]import SMStateMachineChangeStateParams.ts successed!`);
    callback && callback();
};

const initSMState = (callback?: Function) => {
    console.log(`[tool-box]import SMState.ts...`);
    fileUtil.copy(path.join(srcPath, 'SMState.tsz'), path.join(dstPath, 'SMState.ts'));
    console.log(`[tool-box]import SMState.ts successed!`);
    callback && callback();
};

const initSMStateMachine = (callback?: Function) => {
    console.log(`[tool-box]import SMStateMachine.ts...`);
    fileUtil.copy(path.join(srcPath, 'SMStateMachine.tsz'), path.join(dstPath, 'SMStateMachine.ts'));
    console.log(`[tool-box]import SMStateMachine.ts successed!`);
    callback && callback();
};




export const initFileOtherStateMachine = (callback?: Function) => {
    return callSequence([initDir, initSMStateMachineChangeStateParams, initSMState, initSMStateMachine, async (cb?: Function)=>{
        await pathUtil.refreshAsset(`db://assets/bundles/framework/other/state-machine/`);
        cb && cb();
    }, callback]);
};